package com.tbit.uqbike.client.remote.zfb;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.ZhimaCreditScoreBriefGetRequest;
import com.alipay.api.response.ZhimaCreditScoreBriefGetResponse;
import com.tbit.uqbike.client.pojo.ZfbPayConfig;
import com.tbit.uqbike.client.util.DateTimeUtil;
import com.tbit.uqbike.client.util.LogUtil;
import com.tbit.uqbike.client.util.TenpayUtil;

/**
 * 芝麻信用
 *
 * @author Leon
 * 2017年11月9日 下午3:22:38
 */
public class ZMXYClient {
    /**
     * 芝麻信用判断
     *
     * @param zfbPayConfig
     * @param idNO
     * @param name
     * @param score
     * @return
     */
    @SuppressWarnings("deprecation")
    public static Boolean isSupport(ZfbPayConfig zfbPayConfig, String idNO, String name, Integer score) {
        try {
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", zfbPayConfig.getPartner(), zfbPayConfig.getPrivate_key(), "json", "UTF-8", zfbPayConfig.getAli_public_key(), "RSA2");
            ZhimaCreditScoreBriefGetRequest request = new ZhimaCreditScoreBriefGetRequest();
            request.setBizContent("{" +
                    "    \"transaction_id\":\"" + getNonceStr() + "\"," +
                    "    \"product_code\":\"w1010100000000002733\"," +
                    "    \"cert_type\":\"IDENTITY_CARD\"," +
                    "    \"cert_no\":\"" + idNO + "\"," +
                    "    \"name\":\"" + name + "\"," +
                    "    \"admittance_score\":" + score +
                    "  }");
            ZhimaCreditScoreBriefGetResponse response = alipayClient.execute(request);
            LogUtil.info("身份证号：" + idNO + "-->" + response.getBody());
            if (response.isSuccess()) {
                return "Y".equalsIgnoreCase(response.getIsAdmittance());
            } else {
                LogUtil.warn("芝麻信用失败=" + response.getErrorCode() + "," + response.getMsg());
            }
        } catch (Exception e) {
            LogUtil.warn("芝麻信用调用异常=" + e.getMessage());
        }

        return false;
    }


    /**
     * 获取随机字符串
     *
     * @return
     */
    public static String getNonceStr() {
        // 6位随机数
        String strRandom = TenpayUtil.buildRandom(10) + "";
        return "UQBIKEDEZMXY" + DateTimeUtil.getNowTimeyyyyMMdd() + strRandom;
    }

    public static void main(String[] args) throws AlipayApiException {
        try {
            String private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCYl7KKe+O57/JOxtTyuGBEY7xQpIs7PBnFfwsrMA4NdYrzll9Y7tRf2vp3YePPvV3qRSXs0hlEQOMuNQM0UhiY3v5utYpMLW+xAcNpZcCIX+jy9OnHFf14dNWOkM7b8ltU7HuBRiK0xbOX+Bw9BhjejiZUCGRfrkUHmXuE6pcdatXIDU34Zh+s+riuMXpMPdfyqZ9xKLcyzrA3OEcZxxHhd5l6v6iXENOYxuI1+ICuS5W9te/ftdUKfNH+WXnF2h8j9OOD54eyzawrmcvnr4nJ+OKr6si+4nnwJb208H+TlQJlztmmMmWSy5SNjCKVDZ4r151C5vod7X/t4GrxvdNDAgMBAAECggEAGi/WbRurzioWjFxlCS6JHtN66uACIO1Egvc1hHjfwIGOkhUZUdrSyYx79WbwQKN/7YdmZcj8mxEDq9dfMGhyXjHOulMfY8lcZKUn0vuqWf0TrjZHtGnasQRj7e5+AZMLB3z6CQpvRFS1aHwk5HgaZn9ApqU2DJGMuSwm3X2g52Q9aOPq6LoE9s5Lf5rO65eN4QfjSvonkvBCic0R83/wzo8tfKrxMwoHAU3teYErb3hJICUZrqoUHrPg68kjFJMclTHKWBtGL2HEC+Ga0J0leJo230m2WLmLuFY26dSiPRIPUfGEAF4i4xMd5SV89ML93X6MsbGYTD/xbVrNfTHhCQKBgQDJCWXb+7Bf98FiOkNMbonX3q63FVVN1YaTo4tWRrHhuMTjYpH+H05CENuB78GPhRC/+C5aPgobvtU5Mdy265vuWZ2qW+SNVzc1AuK+044sYrkLCuXogwq9Ioa3Eg8weV7WFL6k1Vu4/irW4FMKSwI6uF6qSj/tP1ltR8rZNuoetQKBgQDCT68HigQIexW7SgTtyG7W+uEcgjbpsTr3oiNaMqlKgWxapEo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= new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "2017071907813553", private_key, "json", "UTF-8", public_key, "RSA2");
            ZhimaCreditScoreBriefGetRequest request = new ZhimaCreditScoreBriefGetRequest();
            request.setBizContent("{" +
                    "    \"transaction_id\":\"" + getNonceStr() + "\"," +
                    "    \"product_code\":\"w1010100000000002733\"," +
                    "    \"cert_type\":\"IDENTITY_CARD\"," +
                    "    \"cert_no\":\"331081198608217622\"," +
                    "    \"name\":\"陈娅\"," +
                    "    \"admittance_score\":" + 650 +
                    "  }");
            ZhimaCreditScoreBriefGetResponse response = alipayClient.execute(request);
            LogUtil.info(response.getBody());
        } catch (Exception e) {
            LogUtil.warn("芝麻信用调用异常=" + e.getMessage());
        }
    }

}
